<script setup lang='ts'>
import { exportPageExportHistoryApiPost } from "@/api/fileDown"
import {ElMessage} from "element-plus"
import { ref, reactive } from "vue"

defineOptions({
    name: 'addListAd'
})
/* 
GENERATING(1, "生成中"),
GENERATION_COMPLETED(2, "生成完成"),
GENERATION_FAILED(3, "生成失败"),
 */
const emit = defineEmits(['flash'])
 const fileType:any={
    "1":"生成中",
    "2":"生成完成",
    "3":"生成失败",
 }
const listSearch = reactive<any>({
    pageNum: 1,
    pageSize: 10,
    condition: {
        moduleCode: [

        ]
    },
      orders: ["created_time desc"],
})
const dialogBasic = ref({
    title: "下载列表",
    visible: false,
    width: 1000,
    height: 550,
    showFooter: false,
    marginTop: "10vh",
})
const pageData = reactive({
    tableData: [],
    total: 0,
    LoadIng: false
})
const flash=()=>{
    emit('flash')
}
//显示弹框
const show = async (title: string, type: string, row?: any) => {
    listSearch.condition.moduleCode = [] as any
    listSearch.condition.moduleCode.push(type)
    await getData()
    dialogBasic.value.title = title
    dialogBasic.value.visible = true
}
const getData = async () => {
    pageData.LoadIng = true
    try {
        const res = await exportPageExportHistoryApiPost(listSearch)
        if (res.code == 20000) {
            pageData.tableData = res.data.records
            pageData.LoadIng = false
            pageData.total = res.data.total

        }
    } catch (error) {
        pageData.LoadIng = false

    }

}
const onClose = () => {
    dialogBasic.value.visible = false
    pageData.tableData = []
    pageData.LoadIng = false
    pageData.total = 0
}
const onConfirm = () => {
    dialogBasic.value.visible = false
}
/* 
GENERATING(1, "生成中"),
GENERATION_COMPLETED(2, "生成完成"),
GENERATION_FAILED(3, "生成失败"),
 */
const downList = (row: any) => {
    // 创建隐藏的可下载链接
    if(row.status ==1 ) return ElMessage({type:'warning',message:"文件生成中"})
    if(row.status ==3 ) return ElMessage({type:'warning',message:"文件生成失败"})

    var element = document.createElement('a');

    element.href = row.fileUri;

    element.download = row.fileName;
    element.style.display = 'none';



    // 将其插入到DOM中
    document.body.appendChild(element);

    // 触发点击事件
    element.click();

    // 然后移除
    document.body.removeChild(element);
}
const handleSizeChange = (size: number) => {
    listSearch.pageSize = size
    getData()
}
const handleCurrentChange = (page: number) => {
    listSearch.pageNum = page
    getData()
}

defineExpose({
    show
})
</script>

<template>
    <SysDialog :title="dialogBasic.title" :width="dialogBasic.width" :height="dialogBasic.height"
        :show-footer="dialogBasic.showFooter" :visible="dialogBasic.visible" :margin-top="dialogBasic.marginTop"
        @on-close="onClose" @on-confirm="onConfirm">

        <template v-slot:content>
            <div>
                <div style="display: flex;justify-content: flex-end;margin: 5px 0px;">
                    <el-button type="primary" size="small" @click="flash">刷新</el-button>
                </div>
                <el-table :data="pageData.tableData" v-loading="pageData.LoadIng" style="width: 100%" height="450"
                    stripe :header-cell-style="{ fontSize: '14px', backgroundColor: '#FFF2F3F5', color: '#333' }">
                    <el-table-column prop="applicationTime" label="下载时间" align="center" width="200"
                        show-overflow-tooltip />
                    <el-table-column prop="fileName" label="文件名称" align="center" show-overflow-tooltip />
                    <el-table-column prop="status" label="文件状态" align="center" width="200" show-overflow-tooltip>
                        <template #default="scope">
                            {{ fileType[scope.row.status] }}
                        </template>
                    </el-table-column>
                    <el-table-column fixed="right" label="操作" align="center" width="220">
                        <template #default="scope">
                            <el-button type="primary" size="small" @click="downList(scope.row)" v-if="scope.row.status == 2">下载</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="demo-pagination-block">
                    <el-pagination v-model:currentPage="listSearch.pageNum" v-model:page-size="listSearch.pageSize"
                        :page-sizes="[10, 20, 50, 100]" layout="total, sizes, prev, pager, next, jumper"
                        :total="pageData.total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
                </div>
            </div>

        </template>

    </SysDialog>
</template>

<style lang='scss' scoped>
.demo-pagination-block {
    display: flex;
    justify-content: end;
    margin-top: 10px;
}
</style>
